<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        deepOrderDetailId: {
          label: '订单编号',
          type: 'list',
          list: [],
          placeholder: '请输入订单',
          filterable: true,
          selectCallback: this.orderSelectCallback
        },
        deepProductId: {
          label: '产品',
          type: 'list',
          filterable: true,
          list: [],
          placeholder: '请选择产品',
          addNewItemFunc: this.addNewProduct
        },
        clientId: {
          type: 'list',
          list: [],
          label: '客户名',
          filterable: true,
          placeholder: '请输入客户名',
          addNewItemFunc: this.addNewProduct
        },
        putInTime: {
          label: '入库时间',
          type: 'date',
          showInMultiMode: true,
          placeholder: '请选择入库时间'
        },
        productSource: {
          label: '半成品开源',
          type: 'radio',
          radioBorder: true,
          radioList: [
            {text: '三和', label: 0},
            {text: '外协厂商', label: 1}
          ],
          default: 1
        },
        productNumber: {
          label: '半成品数量',
          type: 'number',
          max: 10000000,
          default: 0
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      },
      editTableConfig: {
        orderDetailId: {
          title: '订单编号',
          type: 'select',
          optionsGet: () => {
            return this.orderDetailList
          },
          valueChangeEvent: this.changeOrderByOrderDetailId
        },
        productId: {
          title: '产品名称',
          type: 'select',
          optionsGet: () => {
            return this.productInfoList
          }
        },
        productNumber: {
          title: '半成品数量',
          type: 'number',
          min: 0,
          default: 0
        },
        remark: {
          title: '备注'
        }
      }
    }
  },
  methods: {
    async init(id, multiAdd) {
      this.dataForm.id = id || 0
      this.initDeepOrderDetailList('deepOrderDetailId')
      this.initDeepProductInfoList()
      this.initClientInfoList()

      const props = {
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        stepsConfig: this.stepsConfig,
        dataRule: this.getNeedDataRule('clientId', 'orderDetailId', 'putInTime', 'productId', 'productNumber', 'status'),
        initApi: 'deepApi/deepSemiFinishedInfo',
        saveApi: 'deepApi/saveDeepSemiFinished',
        updateApi: 'deepApi/updateDeepSemiFinished',
        objectName: 'deepSemiFinished'
      }

      if (multiAdd) {
        mRequest('orderApi/getDeepOrderDetailList', {}, res => {
          this.orderDetailList = this.R.pathOr([], ['data', 'list'], res)
        })
        mRequest('productApi/getProductInfoList', {}, res => {
          this.productInfoList = this.R.pathOr([], ['data', 'list'], res)
        })


        props['multiSaveApi'] = 'deepApi/multipleDeepSemiFinished'
        props['editTableConfig'] = this.editTableConfig
      }

      this.$refs.dialog.initDialog(props)
    },
    changeOrderByOrderDetailId(row) {
      const {orderDetailId} = row

      if (this.R.isNil(orderDetailId) || this.R.equals('', orderDetailId)) {
        return
      }
      mRequest('orderApi/orderDetailInfo', {id: orderDetailId}, (data) => {
        const orderDetail = this.R.pathOr({}, ['data', 'orderDetail'], data)

        row.productId = orderDetail.productId
        row.clientId = orderDetail.clientId
      })
    },
    addNewProduct() {
      const cb = (newValue) => {
        this.initProductInfoList()
        this.dataForm.productId = newValue.id
        this.productSelectCallback(this.dataForm.productId)
      }
      this.$parent.openProductInfoDialog(cb)
    },
    addNewClientInfo() {
      const cb = (newValue) => {
        this.initClientInfoList()
        this.dataForm.clientId = newValue.id
      }
      this.$parent.openClientInfoDialog(cb)
    },
    orderSelectCallback() {
      const deepOrderDetailId = this.dataForm.deepOrderDetailId
      if (this.R.isNil(deepOrderDetailId) || this.R.isEmpty(deepOrderDetailId)) {
        return
      }
      mRequest('deepApi/deepOrderDetailInfo', {id: deepOrderDetailId}, ({data}) => {
        this.dataForm['deepProductId'] = this.R.pathOr(0, ['deepOrderDetail', 'deepProductId'], data)
        this.dataForm['clientId'] = this.R.pathOr(0, ['deepOrderDetail', 'clientId'], data)
      })
    }
  }
}
</script>
